﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.Media;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using SQLite;

namespace Medicine.Models
{
    [Serializable, Table("Items")]
    public class Medicine : BaseModel
    {
        #region 属性
        /// <summary>
        /// 药品名称
        /// </summary>
        [Column("medicinename")]
        public string MedicineName { get; set; }

        /// <summary>
        /// 药品产地
        /// </summary>
        [Column("origin")]
        public string Origin { get; set; }

        /// <summary>
        /// 药品用途
        /// </summary>
        [Column("purposes")]
        public string Purposes { get; set; }

        /// <summary>
        /// 药品用量
        /// </summary>
        [Column("dosage")]
        public string Dosage { get; set; }

        /// <summary>
        /// 到期时间
        /// </summary>
        [Column("expirationdate")]
        public DateTime ExpirationDate { get; set; }

        /// <summary>
        /// 药品数量
        /// </summary>
        [Column("number")]
        public string Number { get; set; }

        /// <summary>
        /// 厂家
        /// </summary>
        [Column("manufacturers")]
        public string Manufacturers { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        [Column("remark")]
        public string Remark { get; set; }

        [Column("extend")]
        public string Extend { get; set; }

        [Column("extend2")]
        public string Extend2 { get; set; }

        [Column("extend3")]
        public string Extend3 { get; set; }
        #endregion

        #region 方法
        /// <summary>
        /// 检查保存数据
        /// </summary>
        /// <returns></returns>
        public NoticeMessage CheckData()
        {
            NoticeMessage noticeMessage = new NoticeMessage();

            if (string.IsNullOrEmpty(this.MedicineName))
            {
                noticeMessage.SetMessage(NoticeEnum.Failed, "药品名称不能为空");
                goto End;
            }

            if (string.IsNullOrEmpty(this.Number))
            {
                noticeMessage.SetMessage(NoticeEnum.Failed, "药品数量不能为空");
                goto End;
            }

            noticeMessage.SetMessage(NoticeEnum.OK, "");

            End:
            return noticeMessage;
        }

        /// <summary>
        /// 自动更新：有不同值，自动覆盖旧值。
        /// </summary>
        /// <param name="newData"></param>
        public void UpateData(Medicine newData)
        {
            if (newData == null)
                return;

            var  properties= this.GetType().GetProperties();
            foreach (var item in properties)
            {
                var oldVal = item.GetValue(this);
                var newVal = item.GetValue(newData);

                if (oldVal == null && newVal == null)
                    continue;
                if (newVal == null)
                    continue;
                if (oldVal.ToString() == newVal.ToString())
                    continue;

                item.SetValue(this, newVal);
            }
        }
        #endregion

        #region 重写方法
        public override bool Equals(object obj)
        {
            if (!(obj is Medicine))
                return false;

            var tmp = obj as Medicine;
            if (tmp == null)
                return false;

            if (tmp.Id == this.Id)
                return true;
            if (tmp.MedicineName == this.MedicineName)
                return true;

            return false;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        #endregion
    }
}